package jerry.cn.rfid_module.Setting;

import android.app.Fragment;
import android.app.FragmentManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import jerry.cn.rfid_module.R;
import jerry.cn.rfid_module.Setting.Common.Constants;
import jerry.cn.rfid_module.Setting.Fragment.MainListFragment;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.util.SettingUtil;

public class S_MainActivity extends BaseActivity implements View.OnClickListener, OnBottomDragListener {

    public static Intent createIntent(Context context) {
        return new Intent(context, S_MainActivity.class);
    }

    private FrameLayout Fl_Connect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main, this);
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }

    @Override
    public void initView() {
        Fl_Connect = findView(R.id.Fl_Connect);
        getSupportFragmentManager().beginTransaction().replace(R.id.Fl_Connect, MainListFragment.createInstance()).commit();
        getSupportFragmentManager().beginTransaction().show(MainListFragment.createInstance());
    }

    @Override
    public void initData() {
        new StaticClass();
        initializeConnectionSettings();
        if (!isBluetoothEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableIntent);
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onDragBottom(boolean rightToLeft) {
        finish();
    }

    /**
     * method to know whether bluetooth is enabled or not
     *
     * @return - true if bluetooth enabled
     * - false if bluetooth disabled
     */
    public static boolean isBluetoothEnabled() {
        return BluetoothAdapter.getDefaultAdapter().isEnabled();
    }

    private void initializeConnectionSettings() {
        SharedPreferences settings = getSharedPreferences(Constants.APP_SETTINGS_STATUS, 0);
        StaticClass.AUTO_DETECT_READERS = settings.getBoolean(Constants.AUTO_DETECT_READERS, true);
        StaticClass.AUTO_RECONNECT_READERS = settings.getBoolean(Constants.AUTO_RECONNECT_READERS, false);
        StaticClass.NOTIFY_READER_AVAILABLE = settings.getBoolean(Constants.NOTIFY_READER_AVAILABLE, false);
        StaticClass.NOTIFY_READER_CONNECTION = settings.getBoolean(Constants.NOTIFY_READER_CONNECTION, false);
        StaticClass.NOTIFY_BATTERY_STATUS = settings.getBoolean(Constants.NOTIFY_BATTERY_STATUS, true);
        StaticClass.EXPORT_DATA = settings.getBoolean(Constants.EXPORT_DATA, false);
    }
}
